package com.app.reportproblem;

import org.json.JSONException;
import org.json.JSONObject;

import com.app.reportproblem.MyUI.KeyboardHelper;
import com.app.reportproblem.Utils.PreferenceHelper;
import com.app.reportproblem.httpclient.HttpClientHelper;
import com.app.reportproblem.httpclient.MyJsonHttpResponseHandler;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class RegisterActivity extends Activity implements OnTouchListener {

	EditText edtName, edtEmail, edtMobile, edtPass;
	ImageButton btnRegister;
	JSONObject data = new JSONObject();
	View main;
	ProgressDialog progressDialog;
	Dialog dS;
	PreferenceHelper preferenceHelper;
	Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		preferenceHelper = new PreferenceHelper(this);

		main = (View) findViewById(R.id.main_register);
		KeyboardHelper.setupUI(RegisterActivity.this, main);

		edtName = (EditText) findViewById(R.id.edt_name);
		edtEmail = (EditText) findViewById(R.id.edt_email);
		edtMobile = (EditText) findViewById(R.id.edt_mobile);
		edtPass = (EditText) findViewById(R.id.edt_pass);
		btnRegister = (ImageButton) findViewById(R.id.btn_register);

		init();
		imeAction();
	}

	void init() {
		btnRegister.setOnTouchListener(this);

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		closeActivityAnim();
	}

	public void closeActivityAnim() {
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}

	void register() {
		progressDialog = ProgressDialog.show(RegisterActivity.this, "",
				getString(R.string.registering));

		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				if (null != progressDialog && progressDialog.isShowing())
					progressDialog.dismiss();
			}
		}, 90000);
		HttpClientHelper httpClientHelper = new HttpClientHelper(
				new MyJsonHttpResponseHandler() {

					@Override
					public void onFailure(Throwable error) {
						progressDialog.dismiss();
						Toast.makeText(RegisterActivity.this,
								getString(R.string.notconnectserver),
								Toast.LENGTH_SHORT).show();
						super.onFailure(error);
					}

					@Override
					public void onSuccess(JSONObject re) {
						try {
							progressDialog.dismiss();
							Log.i("DATA", " " + re.toString());
							int status = re.getInt("status");
							String msg = null;
							switch (status) {
							case 200:
								showDialogSuccess();
								Log.i("DATA", "DANG ki thanh cong");
								break;
							case 400:
								msg = re.getString("message");
								Toast.makeText(RegisterActivity.this, msg,
										Toast.LENGTH_SHORT).show();
								break;
							case 500:
								msg = re.getString("message");
								Toast.makeText(RegisterActivity.this, msg,
										Toast.LENGTH_SHORT).show();
								break;
							default:
								break;
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});

		createData();
		httpClientHelper.regiter(data);
	}

	void createData() {
		try {
			data.put("email", edtEmail.getText().toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			data.put("pass", edtPass.getText().toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			data.put("name", edtName.getText().toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			data.put("mobile", edtMobile.getText().toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	void validateField() {
		if (!edtEmail.getText().toString().matches("")) {
			if (!edtPass.getText().toString().matches("")) {
				register();
			} else {
				Toast.makeText(RegisterActivity.this,
						getString(R.string.passrong), Toast.LENGTH_SHORT)
						.show();
			}

		} else {
			Toast.makeText(RegisterActivity.this,
					getString(R.string.emailrong), Toast.LENGTH_SHORT).show();
		}

	}

	void imeAction() {
		edtName.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {

				return false;
			}
		});

		edtEmail.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				// TODO Auto-generated method stub
				return false;
			}
		});

		edtMobile.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				return false;
			}
		});

		edtPass.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				validateField();
				return false;
			}
		});
	}

	public void showDialogSuccess() {
		dS = new Dialog(RegisterActivity.this);
		dS.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dS.setContentView(R.layout.dialog_success);

		Button btnOk = (Button) dS.findViewById(R.id.btnOk);
		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dS.dismiss();
				finish();
				preferenceHelper.setEmail(edtEmail.getText().toString());
				preferenceHelper.setPass(edtPass.getText().toString());
			}
		});
		dS.show();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn_register:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				validateField();
			}

			break;

		default:
			break;
		}
		return false;
	}

}
